//
//  USChatCell.m
//  us
//
//  Created by karry on 2025/4/6.
//

#import "USChatCell.h"
#import "EaseDateHelper.h"
@implementation USChatCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    self.timeLab = [UILabel new];
    self.timeLab.font = [UIFont systemFontOfSize:14];
    self.timeLab.textColor = UIColor.lightGrayColor;
    self.timeLab.hidden = YES;
    self.timeLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.timeLab];
    
    self.messageContentView = [UIView new];
    self.messageContentView.layer.cornerRadius = 14;
    self.messageContentView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.messageContentView];
    
    self.sendStatusView = [[USSendStatusView alloc]init];
    self.sendStatusView.delegate = self;
    [self.contentView addSubview:self.sendStatusView];
    [self.sendStatusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.messageContentView.mas_left).mas_offset(-8);
        make.centerY.mas_equalTo(self.messageContentView);
        make.width.height.mas_equalTo(20);
    }];
 
    self.unReadView = [[UIView alloc]init];
    [self.contentView addSubview:self.unReadView];
    self.unReadView.layer.cornerRadius = 4;
    self.unReadView.layer.masksToBounds = YES;
    self.unReadView.backgroundColor = UIColor.systemRedColor;
    [self.unReadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.messageContentView.mas_right).mas_offset(8);
        make.centerY.mas_equalTo(self.messageContentView);
        make.width.height.mas_equalTo(8);
    }];
    

    self.avatarImageView = [[CharacterView alloc]initWithFrame:CGRectMake(0, 0, 40, 40) character:[USCartoonCharacter defaultGirlCharacter]];
    [self.contentView addSubview:self.avatarImageView];
    self.avatarImageView.backgroundColor = UIColor.mi_1_color;
    self.avatarImageView.layer.cornerRadius = 20;
    self.avatarImageView.layer.masksToBounds = YES;

    [self.timeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(0);
        make.height.mas_equalTo(0);
    }];
    
    @weakify(self);
    self.avatarImageView.userInteractionEnabled = YES;
    [self.avatarImageView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self);
    
        [self messageAvatarTapAction];
    }];
    
    [self.messageContentView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self);
     
        [self bubbleViewTapAction];
    }];
    
    [self.messageContentView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(bubbleViewLongPressAction:)]];

}
 
- (void)setReadStatusHidden:(BOOL)isHidden {
    self.unReadView.hidden = isHidden;
}

- (void)setMessageViewModel:(USMessageViewModel *)messageViewModel{
    _messageViewModel = messageViewModel;
    _messageViewModel.cell = self;
 
    if (messageViewModel.message.direction == 0) {
        self.avatarImageView.character = USUser.user.character;
    }else{
        self.avatarImageView.character = USUser.user.relCharacter;
    }

    if (self.messageViewModel.showTime) {
        NSTimeInterval msgTime = messageViewModel.message.localTime;
        if (messageViewModel.message.direction == EMMessageDirectionReceive) {
            msgTime = messageViewModel.message.timestamp;//接收方使用服务器消息
        }
        NSString *timeStr = [EaseDateHelper formattedTimeFromTimeInterval:msgTime];
        self.timeLab.text = timeStr;
    }else{
        self.timeLab.text = nil;
    }
    
    self.sendStatusView.status = self.messageViewModel.message.status;
    self.unReadView.hidden = (self.messageViewModel.message.direction == EMMessageDirectionReceive && self.messageViewModel.message.body.type == EMMessageBodyTypeVoice && !self.messageViewModel.message.isReadAcked)? NO : YES;

    
    [self updateLayout];
    
    [self setNeedsLayout];
}

- (void)updateLayout{
    
    if (self.messageViewModel.showTime) {
        
        self.timeLab.hidden = NO;
        [self.timeLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(30);
        }];

    }else{

        self.timeLab.hidden = YES;
        [self.timeLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        
    }
  

    if (self.messageViewModel.message.direction == EMMessageDirectionSend) {
        
        [self.avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-12);
            make.width.height.mas_equalTo(42);
            make.top.mas_equalTo(self.timeLab.mas_bottom).mas_offset(0);
        }];
        
        [self.messageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.avatarImageView);
            make.right.mas_equalTo(self.avatarImageView.mas_left).mas_offset(-8);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-16).priorityLow(); // ✅
            make.width.mas_greaterThanOrEqualTo(30).priority(250);
            make.left.mas_greaterThanOrEqualTo(80).priority(250);
            make.height.mas_greaterThanOrEqualTo(30).priority(250);
        }];
             
    }else{
        
        [self.avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(12);
            make.width.height.mas_equalTo(42);
            make.top.mas_equalTo(self.timeLab.mas_bottom).mas_offset(0);
        }];
        
        [self.messageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.avatarImageView);

            // 左边贴着头像右边
            make.left.mas_equalTo(self.avatarImageView.mas_right).mas_offset(8);

            // 右边最多到 -80，优先级低一点
            make.right.mas_lessThanOrEqualTo(-80).priority(250);

            // 宽度最小限制 30，避免太窄
            make.width.mas_greaterThanOrEqualTo(30).priority(250);

            // 底部固定
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-16).priorityLow();
            
            make.height.mas_greaterThanOrEqualTo(30).priority(250);
        }];
        
        
    }
    
}
#pragma mark - USSendStatusViewDelegate
- (void)sendStatusViewDidTapRetry:(USSendStatusView *)view {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(messageCellDidResend:)]) {
        [self.delegate messageCellDidResend:self];
    }
}

#pragma mark - Action
//头像点击
- (void)messageAvatarTapAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(avatarDidSelected:)]) {
        [self.delegate avatarDidSelected:self.messageViewModel];
    }
}

//气泡点击
- (void)bubbleViewTapAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(messageCellDidSelected:)]) {
        [self.delegate messageCellDidSelected:self];
    }
}

//气泡长按
- (void)bubbleViewLongPressAction:(UILongPressGestureRecognizer *)aLongPress{
    if (aLongPress.state == UIGestureRecognizerStateBegan) {
        if (self.messageViewModel.message.body.type == EMMessageBodyTypeText) {
            
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(messageCellDidLongPress:bubbleView:)]) {
            [self.delegate messageCellDidLongPress:self bubbleView:self.messageContentView];
        }
      
    }
    //[aLongPress release];
}

@end
